/*
  Part of the MasterTool3D project - http://code.google.com/p/master-tool-3d

  Copyright (c) 2011 Ingo Pueschl

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation, version 2.1.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package techjit.util;

import processing.core.PApplet;

public abstract class LogManager {

	// FIELDS
	private static String indent = "";

	private static int lastLoggingFrame = -1;

	// METHODS
	public static void beginObjectConstructionEntry() {
		log("Constructed:");
	}

	private static void currentFrameLogging(PApplet p) {
		if (RuntimeParams.LOGGING__LOG_FRAME_WHEN_INTERVAL_IS_REACHED) {
			log("< ### "
					+ p.frameCount
					+ "|"
					+ RuntimeParams.SKETCH__FRAME_RATE
					+ "|"
					+ p.frameRate
					+ " (number of current frame | frames per second setting | average frames per second) ###>");
		}

	}

	private static void debug(String s) {
		log("#D> " + new Throwable().getStackTrace()[2].getFileName() + "|"
				+ new Throwable().getStackTrace()[2].getMethodName() + "|"
				+ new Throwable().getStackTrace()[2].getLineNumber() + ": " + s);
	}

	public static void debugALL(String stringValue) {
		debug(stringValue);
	}

	public static void debugCS(String stringValue) {
		if (RuntimeParams.AUTHOR__CS) {
			debug(stringValue);
		}
	}

	public static void debugER(String stringValue) {
		if (RuntimeParams.AUTHOR__ER) {
			debug(stringValue);
		}
	}

	public static void debugHK(String stringValue) {
		if (RuntimeParams.AUTHOR__HK) {
			debug(stringValue);
		}
	}

	public static void debugIP(String stringValue) {
		if (RuntimeParams.AUTHOR__IP) {
			debug(stringValue);
		}
	}

	public static void debugIP(String stringValue, PApplet p) {
		if (RuntimeParams.AUTHOR__IP) {
			debug(stringValue);
			p.printMatrix();
		}
	}

	public static void debugJR(String stringValue) {
		if (RuntimeParams.AUTHOR__JR) {
			debug(stringValue);
		}
	}

	public static void debugTH(String stringValue) {
		if (RuntimeParams.AUTHOR__TH) {
			debug(stringValue);
		}
	}

	public static void debugTM(String stringValue) {
		if (RuntimeParams.AUTHOR__TM) {
			debug(stringValue);
		}
	}

	public static void deindent() {
		indent = indent.substring(0, indent.length() - 4);
	}

	public static void endObjectConstructionEntry() {
		log("");
	}

	private static void error(String s) {
		log("#E> " + s);
	}

	public static void errorALL(String stringValue) {
		log(stringValue);
	}

	public static void errorCS(String stringValue) {
		if (RuntimeParams.AUTHOR__CS) {
			error(stringValue);
		}
	}

	public static void errorER(String stringValue) {
		if (RuntimeParams.AUTHOR__ER) {
			error(stringValue);
		}
	}

	public static void errorHK(String stringValue) {
		if (RuntimeParams.AUTHOR__HK) {
			error(stringValue);
		}
	}

	public static void errorIP(String stringValue) {
		if (RuntimeParams.AUTHOR__IP) {
			error(stringValue);
		}
	}

	public static void errorJR(String stringValue) {
		if (RuntimeParams.AUTHOR__JR) {
			error(stringValue);
		}
	}

	public static void errorTH(String stringValue) {
		if (RuntimeParams.AUTHOR__TH) {
			error(stringValue);
		}
	}

	public static void errorTM(String stringValue) {
		if (RuntimeParams.AUTHOR__TM) {
			error(stringValue);
		}
	}

	public static void finishEntry() {
		log("");
	}

	public static void finishEntry(PApplet p) {
		log(p, "");
	}

	public static void indent() {
		indent += "    ";
	}

	public static void info(String s) {
		log("#I> " + s);
	}

	public static void infoALL(String stringValue) {
		info(stringValue);
	}

	public static void infoCS(String stringValue) {
		if (RuntimeParams.AUTHOR__CS) {
			info(stringValue);
		}
	}

	public static void infoER(String stringValue) {
		if (RuntimeParams.AUTHOR__ER) {
			info(stringValue);
		}
	}

	public static void infoHK(String stringValue) {
		if (RuntimeParams.AUTHOR__HK) {
			info(stringValue);
		}
	}

	public static void infoIP(String stringValue) {
		if (RuntimeParams.AUTHOR__IP) {
			info(stringValue);
		}
	}

	public static void infoJR(String stringValue) {
		if (RuntimeParams.AUTHOR__JR) {
			info(stringValue);
		}
	}

	public static void infoTH(String stringValue) {
		if (RuntimeParams.AUTHOR__TH) {
			info(stringValue);
		}
	}

	public static void infoTM(String stringValue) {
		if (RuntimeParams.AUTHOR__TM) {
			info(stringValue);
		}
	}

	public static void log(PApplet p, String string) {
		if (reachedLoggingFrame(p)) {
			log(string);
		}
	}

	private static void log(String stringValue) {
		System.out.println(indent + stringValue);
	}

	public static void logALL(String stringValue) {
		log(stringValue);
	}

	public static void logCS(String stringValue) {
		if (RuntimeParams.AUTHOR__CS) {
			log(stringValue);
		}
	}

	public static void logER(String stringValue) {
		if (RuntimeParams.AUTHOR__ER) {
			log(stringValue);
		}
	}

	public static void logHK(String stringValue) {
		if (RuntimeParams.AUTHOR__HK) {
			log(stringValue);
		}
	}

	public static void logIP(String stringValue) {
		if (RuntimeParams.AUTHOR__IP) {
			log(stringValue);
		}
	}

	public static void logJR(String stringValue) {
		if (RuntimeParams.AUTHOR__JR) {
			log(stringValue);
		}
	}

	public static void logTH(String stringValue) {
		if (RuntimeParams.AUTHOR__TH) {
			log(stringValue);
		}
	}

	public static void logTM(String stringValue) {
		if (RuntimeParams.AUTHOR__TM) {
			log(stringValue);
		}
	}

	public static boolean reachedLoggingFrame(PApplet p) {
		int currentFrame = p.frameCount;

		if (currentFrame == lastLoggingFrame) {
			return true;
		} else {
			if (RuntimeParams.LOGGING__INTERVAL_THAT_MUST_BE_REACHED_TO_LOG_FRAME == 0) {
				return false;
			} else {
				if (currentFrame
						% RuntimeParams.LOGGING__INTERVAL_THAT_MUST_BE_REACHED_TO_LOG_FRAME == 0) {
					lastLoggingFrame = currentFrame;
					LogManager.currentFrameLogging(p);
					return true;
				} else {
					return false;
				}
			}
		}
	}

	private static void warning(String s) {
		log("#W> " + s);
	}

	public static void warningALL(String stringValue) {
		warning(stringValue);
	}

	public static void warningCS(String stringValue) {
		if (RuntimeParams.AUTHOR__CS) {
			warning(stringValue);
		}
	}

	public static void warningER(String stringValue) {
		if (RuntimeParams.AUTHOR__ER) {
			warning(stringValue);
		}
	}

	public static void warningHK(String stringValue) {
		if (RuntimeParams.AUTHOR__HK) {
			warning(stringValue);
		}
	}

	public static void warningIP(String stringValue) {
		if (RuntimeParams.AUTHOR__IP) {
			warning(stringValue);
		}
	}

	public static void warningJR(String stringValue) {
		if (RuntimeParams.AUTHOR__JR) {
			warning(stringValue);
		}
	}

	public static void warningTH(String stringValue) {
		if (RuntimeParams.AUTHOR__TH) {
			warning(stringValue);
		}
	}

	public static void warningTM(String stringValue) {
		if (RuntimeParams.AUTHOR__TM) {
			warning(stringValue);
		}
	}

}
